package com.example.bike201308;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.view.View;
 

public class CompassView extends View {
    private Drawable mCompass;
    private float mAzimuth = 0;
    private int PADDING = 2;
    private boolean mSideBottom;
 
    public CompassView(Context ctx) {
        super(ctx);
 
        this.mCompass = ctx.getResources().getDrawable(R.drawable.arrow_n);
    }
    
    public CompassView(Context ctx, final boolean msideBottom) {
        super(ctx);
 
        this.mCompass = ctx.getResources().getDrawable(R.drawable.arrow_n);
        this.mSideBottom = msideBottom;
    }
 
    protected void onDraw(Canvas canvas) {
        canvas.save();
        if (mSideBottom) {
            canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth()
                    / 2, PADDING + mCompass.getMinimumHeight() / 2);
            mCompass.setBounds(PADDING, PADDING, PADDING
                    + mCompass.getMinimumWidth(), PADDING
                    + mCompass.getMinimumHeight());
        } else {
            canvas.rotate(360 - mAzimuth, PADDING + mCompass.getMinimumWidth()
                    / 2, this.getHeight() - mCompass.getMinimumHeight() / 2
                    - PADDING);
            mCompass.setBounds(PADDING, this.getHeight()
                    - mCompass.getMinimumHeight() - PADDING, PADDING
                    + mCompass.getMinimumWidth(), this.getHeight() - PADDING);
        }
 
        super.onDraw(canvas);
    }
 
    public void setAzimuth(float aAzimuth) {
        mAzimuth = aAzimuth;
    }
 
}